package beanstao.util.random;

public class RecurArrayRandom<T> implements IRandom<T>
{

	private final T[] array;
	private int len;
	private final java.util.Random r = new java.util.Random();

	public RecurArrayRandom(T[] array)
	{
		this.array = array;
		this.len = array.length;
	}

	public T next()
	{
		if (this.len <= 0)
		{
			this.len = this.array.length;
		}
		if (this.len == 1)
		{
			return this.array[--this.len];
		}
		int index = this.r.nextInt(this.len);
		if (index == this.len - 1)
		{
			return this.array[--this.len];
		}
		T c = this.array[index];
		this.array[index] = this.array[--this.len];
		this.array[this.len] = c;
		return c;
	}

}
